USE [BS2]
GO
/****** Object:  View [dbo].[vw_VouchersDetail]    Script Date: 04/01/2015 21:16:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[vw_VouchersDetail]
AS
SELECT     dbo.VoucherDetails.VoucherID, dbo.Voucher.VoucherNo, dbo.Voucher.VoucherDate, dbo.VoucherDetails.AccountNo, dbo.Accounts.AccountTitle, 
                      dbo.SessionInfo.SessionTitle, dbo.SessionInfo.SessionFrom, dbo.SessionInfo.SessionTo, dbo.SessionInfo.CurrentSession, 
                      dbo.VoucherDetails.Remarks, dbo.VoucherDetails.Amount AS Debit, 0 AS Credit, dbo.VoucherDetails.DrOrCr, dbo.VoucherDetails.VoucherDetailID, 
                      dbo.Voucher.Remarks AS MasterRemarks, dbo.Voucher.SessionID, dbo.Accounts.ParentAccount, dbo.Voucher.BankAccountID, 
                      ISNULL(dbo.Voucher.CustomerName, '') AS CustomerName, dbo.Voucher.Expense, dbo.Voucher.Discount, dbo.vw_Items.GroupID, 
                      dbo.vw_Items.GroupName, dbo.VoucherDetails.ItemID, dbo.vw_Items.ItemName, dbo.vw_Items.InUnit, dbo.VoucherDetails.BatchNo, 
                      dbo.VoucherDetails.Qty + dbo.VoucherDetails.Bonus AS Qty, dbo.VoucherDetails.Qty + dbo.VoucherDetails.Bonus AS InQty, 0 AS OutQty, 
                      dbo.VoucherDetails.Rate, dbo.VoucherDetails.Amount
FROM         dbo.VoucherDetails LEFT OUTER JOIN
                      dbo.vw_Items ON dbo.VoucherDetails.ItemID = dbo.vw_Items.ItemID LEFT OUTER JOIN
                      dbo.Accounts ON dbo.VoucherDetails.AccountNo = dbo.Accounts.AccountNo LEFT OUTER JOIN
                      dbo.Voucher ON dbo.VoucherDetails.VoucherID = dbo.Voucher.VoucherID LEFT OUTER JOIN
                      dbo.SessionInfo ON dbo.Voucher.SessionID = dbo.SessionInfo.SessionID
WHERE     (dbo.VoucherDetails.DrOrCr = N'Dr')
UNION ALL
SELECT     VoucherDetails_1.VoucherID, Voucher_1.VoucherNo, Voucher_1.VoucherDate, VoucherDetails_1.AccountNo, Accounts_1.AccountTitle, 
                      SessionInfo_1.SessionTitle, SessionInfo_1.SessionFrom, SessionInfo_1.SessionTo, SessionInfo_1.CurrentSession, VoucherDetails_1.Remarks, 
                      0 AS Debit, VoucherDetails_1.Amount AS Credit, VoucherDetails_1.DrOrCr, VoucherDetails_1.VoucherDetailID, Voucher_1.Remarks AS MasterRemarks, 
                      Voucher_1.SessionID, Accounts_1.ParentAccount, Voucher_1.BankAccountID, ISNULL(Voucher_1.CustomerName, '') AS CustomerName, 
                      Voucher_1.Expense, Voucher_1.Discount, vw_Items_1.GroupID, vw_Items_1.GroupName, VoucherDetails_1.ItemID, vw_Items_1.ItemName, 
                      vw_Items_1.InUnit, VoucherDetails_1.BatchNo, VoucherDetails_1.Qty + VoucherDetails_1.Bonus AS Qty, 0 AS InQty, 
                      VoucherDetails_1.Qty + VoucherDetails_1.Bonus AS OutQty, VoucherDetails_1.Rate, VoucherDetails_1.Amount
FROM         dbo.VoucherDetails AS VoucherDetails_1 LEFT OUTER JOIN
                      dbo.vw_Items AS vw_Items_1 ON VoucherDetails_1.ItemID = vw_Items_1.ItemID LEFT OUTER JOIN
                      dbo.Accounts AS Accounts_1 ON VoucherDetails_1.AccountNo = Accounts_1.AccountNo LEFT OUTER JOIN
                      dbo.Voucher AS Voucher_1 ON VoucherDetails_1.VoucherID = Voucher_1.VoucherID LEFT OUTER JOIN
                      dbo.SessionInfo AS SessionInfo_1 ON Voucher_1.SessionID = SessionInfo_1.SessionID
WHERE     (VoucherDetails_1.DrOrCr = N'Cr')
